<?php

namespace app\http\Controllers\Manage;

use app\BaseController as Controller;
use app\common\lib\Captcha;
use app\common\lib\UploadFile;
use think\App;
use think\facade\View;
use think\Request;


class BaseController extends Controller
{

    const TEMPLATE_TYPE_LOGIN = 1;
    const TEMPLATE_TYPE_SKIN = 2;
    const TEMPLATE_TYPE_COMMON = 3;

    protected $skinThemes = 'default';

    protected $accountId = 0;
    protected $accountInfo = [];

    public function __construct(App $app, Request $request)
    {
        parent::__construct($app);
        View::assign([
            'static_version' => '1.0.7',
            'site_name' => '管理端',
            '__STATIC__' => '/static/manage',
            '__COMMON__' => '/static/common',
            'skinThemes' => $this->skinThemes
        ]);
        $accountInfo = $request->account;
        if ($accountInfo) {
            $this->accountId = $accountInfo['account_id'];
            $this->accountInfo = $accountInfo;
        }

    }


    /**
     * 视图渲染并传参
     */
    protected function view($page, $data = [], $templateType = self::TEMPLATE_TYPE_SKIN)
    {
        $bladePath = 'Manage';
        switch ($templateType) {
            case self::TEMPLATE_TYPE_LOGIN: //登录模板
                $bladePath .= DIRECTORY_SEPARATOR . 'login' . DIRECTORY_SEPARATOR;
                break;
            case self::TEMPLATE_TYPE_SKIN://皮肤
                $bladePath .= DIRECTORY_SEPARATOR . 'themes' . DIRECTORY_SEPARATOR . $this->skinThemes . DIRECTORY_SEPARATOR;
                break;
            case self::TEMPLATE_TYPE_COMMON://该端公共视图
                $bladePath .= DIRECTORY_SEPARATOR;
                break;
        }
        return View::fetch($bladePath . $page, $data);
    }


    /**
     * 公用错误提示页面
     */
    protected function showErrorPage($status, $msg, $data = [])
    {
        $bladePath = 'Manage' . DIRECTORY_SEPARATOR;
        $data = [
            'status' => $status,
            'msg' => $msg,
            'data' => $data
        ];
        return View::fetch($bladePath . 'error', $data);
    }

    /**
     * @param $dir
     * @param $allowExt
     * @param $maxSize
     * @param $resizeData
     * @param $param
     * @return \Illuminate\Http\JsonResponse
     */
    public function uploadFile(string $dir, array $allowExt = [], int $maxSize = 0, array $resizeData = [], array $params = [])
    {
        $uploadFile = new UploadFile();
        $path = trim($dir, '/');
        $params['path'] = $path;
        $params['fileField'] = 'file';
        $params['resizeData'] = $resizeData;
        $res = $uploadFile->upload($params, $allowExt, $maxSize);
        return apiResponse($res['status'], $res['msg'], $res['data']);
    }

    /**
     * 创建验证码
     * @param array $param
     * @date 2021-01-19 20:33
     */
    public function createCaptcha($captchaCode = '', $param = [])
    {
        return Captcha::create($captchaCode, $param);
    }
}